dynamic class classes.GameWW
{
    var AI;
    var AUTOPLAYER;
    var END_TURN_MC;
    var FIGHTBOX;
    var GAME_MC;
    var MAP;
    var PLAYERS_CACHES;
    var PLAYERS_NO;
    var PLAYER_MOVE;
    var TRACER;
    var UNITS_LIST;
    var UNITS_MC;
    var WAITER;
    var counter;
    var ct;
    var maxArea;
    var onEnterFrame;
    var rubbish_arr;

    function GameWW(gmc, umc, etmc, wt, ul, fb)
    {
        this.GAME_MC = gmc;
        this.UNITS_MC = umc;
        this.WAITER = wt;
        this.UNITS_LIST = ul;
        this.END_TURN_MC = etmc;
        this.FIGHTBOX = fb;
        this.AUTOPLAYER = false;
        this.MAP = new classes.Map(this.GAME_MC, this);
        this.AI = new classes.GameAI(this);
        this.counter = 0;
        this.TRACER = _root.createEmptyMovieClip("tracer", 1);
        this.PLAYER_MOVE = 1;
    }

    function newGame(players, callBack)
    {
        if (this.WAITER.status == "ready_off") 
        {
            var root = this;
            this.PLAYERS_NO = players;
            this.PLAYERS_CACHES = new Array();
            var i = 0;
            while (i < this.PLAYERS_NO) 
            {
                this.PLAYERS_CACHES.push(0);
                ++i;
            }
            this.WAITER.show();
            this.GAME_MC.onEnterFrame = function ()
            {
                with (root)
                {
                    if (WAITER.status == "ready_on") 
                    {
                        MAP.createMap(PLAYERS_NO);
                        counter = MAP.TERRITORIES_ARR.length + 10;
                        arrangeUnits();
                        WAITER.hide();
                        var refArr = new Array();
                        var i = 1;
                        while (i <= PLAYERS_NO) 
                        {
                            refArr.push(unitsToAdd(i));
                            ++i;
                        }
                        UNITS_LIST.setList(MAP.TERRITORIES_ARR, refArr, PLAYERS_NO);
                    }
                }
                if (root.WAITER.status == "ready_off") 
                {
                    callBack();
                    delete this.onEnterFrame;
                }
            }
            ;
        }
    }

    function init()
    {
        this.UNITS_LIST.selectPlayer(this.PLAYER_MOVE);
        if (this.PLAYER_MOVE != 1 || this.AUTOPLAYER) 
        {
            this.AI.cleverMove(this.PLAYER_MOVE);
            return;
        }
        this.END_TURN_MC.gotoAndPlay(2);
        this.MAP.startInterface();
    }

    function nextPlayer()
    {
        ++this.PLAYER_MOVE;
        if (this.PLAYER_MOVE > this.PLAYERS_NO) 
        {
            this.PLAYER_MOVE = 1;
        }
        if (this.PLAYER_MOVE != 1 || this.AUTOPLAYER) 
        {
            this.AI.cleverMove(this.PLAYER_MOVE);
        }
        else 
        {
            this.END_TURN_MC.gotoAndPlay(2);
            this.MAP.startInterface();
        }
        if (this.PLAYER_MOVE == 1) 
        {
            this.FIGHTBOX.resetSkipMode();
        }
        this.UNITS_LIST.selectPlayer(this.PLAYER_MOVE);
    }

    function isGameFinished()
    {
        var __reg3 = 0;
        var __reg2 = 0;
        while (__reg2 < this.MAP.TERRITORIES_ARR.length) 
        {
            if (this.MAP.TERRITORIES_ARR[__reg2].player == 1) 
            {
                ++__reg3;
            }
            ++__reg2;
        }
        if (__reg3 == this.MAP.TERRITORIES_ARR.length) 
        {
            this.PLAYER_WIN();
            return true;
        }
        if (__reg3 == 0) 
        {
            this.GAME_OVER();
            return true;
        }
        return false;
    }

    function PLAYER_WIN()
    {
        _root.YOUWIN_MC._visible = true;
        _root.YOUWIN_MC.gotoAndPlay(2);
    }

    function GAME_OVER()
    {
        _root.GAMEOVER_MC._visible = true;
        _root.GAMEOVER_MC.gotoAndPlay(2);
    }

    function arrangeUnits()
    {
        var __reg9 = [0, 0, 0, 0, 0, 0, 0, 0];
        var __reg8 = new Array();
        this.clearAll();
        this.rubbish_arr = new Array();
        var __reg3 = 0;
        while (__reg3 < this.MAP.TERRITORIES_ARR.length) 
        {
            var __reg4 = this.MAP.TERRITORIES_ARR[__reg3].arr;
            var __reg7 = 0;
            var __reg6 = 0;
            var __reg2 = 0;
            while (__reg2 < __reg4.length) 
            {
                __reg7 = __reg7 + __reg4[__reg2]._x;
                __reg6 = __reg6 + __reg4[__reg2]._y;
                ++__reg2;
            }
            __reg7 = __reg7 / __reg4.length;
            __reg6 = __reg6 / __reg4.length;
            this.counter = __reg3;
            var __reg5 = this.UNITS_MC.attachMovie("UNIT", "unit_" + this.counter, 2 + this.counter * 2);
            ++this.counter;
            __reg5._x = __reg7;
            __reg5._y = __reg6;
            __reg5.setUnit(this.MAP.TERRITORIES_ARR[__reg3].army, this.MAP.TERRITORIES_ARR[__reg3].unitsNO, this.MAP.TERRITORIES_ARR[__reg3].player);
            this.MAP.TERRITORIES_ARR[__reg3].unitsMC = __reg5;
            __reg8.push(__reg5);
            this.rubbish_arr.push(__reg5);
            ++__reg3;
        }
        this.unitsSmartDepths(__reg8);
    }

    function unitsSmartDepths(unitsArr)
    {
        var __reg5 = 0;
        while (__reg5 < unitsArr.length) 
        {
            var __reg2 = unitsArr.length - 1;
            while (__reg2 > __reg5) 
            {
                if (unitsArr[__reg2 - 1]._y > unitsArr[__reg2]._y) 
                {
                    var __reg4 = unitsArr[__reg2];
                    unitsArr[__reg2] = unitsArr[__reg2 - 1];
                    unitsArr[__reg2 - 1] = __reg4;
                }
                --__reg2;
            }
            ++__reg5;
        }
        __reg5 = 0;
        for (;;) 
        {
            if (__reg5 >= unitsArr.length) 
            {
                return;
            }
            unitsArr[__reg5].swapDepths(2 + this.counter * 2);
            ++this.counter;
            ++__reg5;
        }
    }

    function showBiggestArea()
    {
        var __reg7 = new Array();
        var __reg3 = 0;
        while (__reg3 < this.MAP.TERRITORIES_ARR.length) 
        {
            if (this.MAP.TERRITORIES_ARR[__reg3].player == this.PLAYER_MOVE) 
            {
                __reg7.push(this.MAP.TERRITORIES_ARR[__reg3]);
            }
            ++__reg3;
        }
        var __reg4 = new Array();
        __reg3 = 0;
        while (__reg3 < __reg7.length) 
        {
            var __reg6 = __reg7[__reg3];
            if (this.isThere(__reg6, __reg4) == false) 
            {
                var __reg5 = new Array();
                this.generate(__reg6, __reg5);
                __reg4.push(__reg5);
            }
            ++__reg3;
        }
        this.maxArea = new Array();
        __reg3 = 0;
        while (__reg3 < __reg4.length) 
        {
            if (__reg4[__reg3].length > this.maxArea.length) 
            {
                this.maxArea = __reg4[__reg3];
            }
            ++__reg3;
        }
        __reg3 = 0;
        while (__reg3 < this.maxArea.length) 
        {
            this.MAP.lightOnTerritory(this.maxArea[__reg3]);
            ++__reg3;
        }
        var __reg9 = 0;
        var __reg8 = 0;
        __reg3 = 0;
        while (__reg3 < this.maxArea.length) 
        {
            __reg9 = __reg9 + this.maxArea[__reg3].unitsMC._x;
            __reg8 = __reg8 + this.maxArea[__reg3].unitsMC._y;
            ++__reg3;
        }
        __reg9 = __reg9 / this.maxArea.length;
        __reg8 = __reg8 / this.maxArea.length;
        var __reg10 = this.UNITS_MC.attachMovie("CLOUD", "CLOUD", this.UNITS_MC.getNextHighestDepth());
        __reg10.toAdd = this.maxArea.length;
        __reg10._x = __reg9;
        __reg10._y = __reg8;
        _global.setTimeout(mx.utils.Delegate.create(this, this.hideBiggestArea), 1200);
    }

    function hideBiggestArea()
    {
        var __reg2 = 0;
        for (;;) 
        {
            if (__reg2 >= this.maxArea.length) 
            {
                return;
            }
            this.MAP.lightOffTerritory(this.maxArea[__reg2]);
            ++__reg2;
        }
    }

    function addUnits()
    {
        var __reg12 = this.unitsToAdd(this.PLAYER_MOVE) + this.PLAYERS_CACHES[this.PLAYER_MOVE - 1];
        var __reg5 = new Array();
        var __reg6 = 0;
        while (__reg6 < this.MAP.TERRITORIES_ARR.length) 
        {
            if (this.MAP.TERRITORIES_ARR[__reg6].player == this.PLAYER_MOVE) 
            {
                __reg5.push(this.MAP.TERRITORIES_ARR[__reg6]);
            }
            ++__reg6;
        }
        var __reg4 = new Array();
        __reg6 = 0;
        while (__reg6 < __reg5.length) 
        {
            __reg4.push(0);
            ++__reg6;
        }
        var __reg11 = __reg5.length;
        while (__reg12 > 0 && __reg11 > 0) 
        {
            __reg11 = __reg5.length;
            __reg6 = 0;
            while (__reg6 < __reg4.length) 
            {
                if (__reg4[__reg6] + __reg5[__reg6].unitsNO >= 8) 
                {
                    --__reg11;
                }
                ++__reg6;
            }
            var __reg7 = this.random2(__reg5.length - 1);
            var __reg9 = false;
            __reg6 = 0;
            while (__reg6 < __reg4.length && !__reg9) 
            {
                if (__reg4[(__reg7 + __reg6) % __reg4.length] + __reg5[(__reg7 + __reg6) % __reg4.length].unitsNO < 8) 
                {
                    ++__reg4[(__reg7 + __reg6) % __reg4.length];
                    --__reg12;
                    __reg9 = true;
                }
                ++__reg6;
            }
        }
        if (__reg12 > 32) 
        {
            __reg12 = 32;
        }
        if (__reg12 > 0 && this.PLAYER_MOVE == 1) 
        {
            _root.EXTRA_UNITS.showExtra = __reg12;
            _root.EXTRA_UNITS.gotoAndPlay(2);
        }
        this.PLAYERS_CACHES[this.PLAYER_MOVE - 1] = __reg12;
        var __reg14 = 0;
        __reg6 = 0;
        while (__reg6 < __reg5.length) 
        {
            if (__reg4[__reg6] > 0) 
            {
                var __reg8 = this.UNITS_MC.attachMovie("UNITS_ADDER", "adder_to_" + __reg5[__reg6].unitsMC._name, __reg5[__reg6].unitsMC.getDepth() - 1);
                __reg5[__reg6].unitsNO = __reg5[__reg6].unitsNO + __reg4[__reg6];
                __reg8._x = __reg5[__reg6].unitsMC._x + 10;
                __reg8._y = __reg5[__reg6].unitsMC._y;
                __reg8.unitsMC = __reg5[__reg6].unitsMC;
                __reg8.army = __reg5[__reg6].army;
                __reg8.newUnitsNO = __reg5[__reg6].unitsNO;
                __reg8.player = __reg5[__reg6].player;
            }
            ++__reg6;
        }
        _global.SOUNDS.playSound("przemarsz - do uzupełniania jednostek.wav");
        var __reg13 = false;
        for (;;) 
        {
            if (__reg13) 
            {
                return;
            }
            var __reg10 = this.PLAYER_MOVE + 1;
            if (__reg10 > this.PLAYERS_NO) 
            {
                __reg10 = 1;
            }
            if (this.countPlayerUnits(this.MAP.TERRITORIES_ARR, __reg10) > 0) 
            {
                _global.setTimeout(mx.utils.Delegate.create(this, this.nextPlayer), 2000);
                __reg13 = true;
            }
            else 
            {
                ++this.PLAYER_MOVE;
            }
        }
    }

    function unitsToAdd(player)
    {
        var __reg6 = new Array();
        var __reg2 = 0;
        while (__reg2 < this.MAP.TERRITORIES_ARR.length) 
        {
            if (this.MAP.TERRITORIES_ARR[__reg2].player == player) 
            {
                __reg6.push(this.MAP.TERRITORIES_ARR[__reg2]);
            }
            ++__reg2;
        }
        var __reg3 = new Array();
        __reg2 = 0;
        while (__reg2 < __reg6.length) 
        {
            var __reg5 = __reg6[__reg2];
            if (this.isThere(__reg5, __reg3) == false) 
            {
                var __reg4 = new Array();
                this.generate(__reg5, __reg4);
                __reg3.push(__reg4);
            }
            ++__reg2;
        }
        var __reg7 = 0;
        __reg2 = 0;
        while (__reg2 < __reg3.length) 
        {
            if (__reg3[__reg2].length > __reg7) 
            {
                __reg7 = __reg3[__reg2].length;
            }
            ++__reg2;
        }
        return __reg7;
    }

    function generate(terr, arrToFill)
    {
        if (this.isThere2(terr, arrToFill) == false) 
        {
            arrToFill.push(terr);
            var __reg3 = this.neighboursAreasArr(terr.arr);
            var __reg2 = 0;
            for (;;) 
            {
                if (__reg2 >= __reg3.length) 
                {
                    return;
                }
                this.generate(__reg3[__reg2], arrToFill);
                ++__reg2;
            }
        }
    }

    function isThere2(terr, arr)
    {
        var __reg1 = 0;
        while (__reg1 < arr.length) 
        {
            if (arr[__reg1] == terr) 
            {
                return true;
            }
            ++__reg1;
        }
        return false;
    }

    function isThere(terr, arr)
    {
        var __reg2 = 0;
        while (__reg2 < arr.length) 
        {
            var __reg1 = 0;
            while (__reg1 < arr[__reg2].length) 
            {
                if (arr[__reg2][__reg1] == terr) 
                {
                    return true;
                }
                ++__reg1;
            }
            ++__reg2;
        }
        return false;
    }

    function neighboursAreasArr(terrArr)
    {
        var __reg6 = new Array();
        var __reg7 = 0;
        while (__reg7 < terrArr.length) 
        {
            var __reg4 = terrArr[__reg7];
            var __reg3 = [__reg4.TL, __reg4.L, __reg4.TR, __reg4.BR, __reg4.R, __reg4.BL];
            var __reg1 = 0;
            while (__reg1 < __reg3.length) 
            {
                if (__reg3[__reg1]._territory != __reg4._territory && __reg3[__reg1]._territory.player == __reg4._territory.player) 
                {
                    var __reg5 = false;
                    var __reg2 = 0;
                    while (__reg2 < __reg6.length && !__reg5) 
                    {
                        if (__reg6[__reg2] == __reg3[__reg1]._territory) 
                        {
                            __reg5 = true;
                        }
                        ++__reg2;
                    }
                    if (!__reg5) 
                    {
                        __reg6.push(__reg3[__reg1]._territory);
                    }
                }
                ++__reg1;
            }
            ++__reg7;
        }
        return __reg6;
    }

    function fightFinished()
    {
        this.MAP.winnerFound();
        if (this.PLAYER_MOVE == 1) 
        {
            this.MAP.selectA();
        }
        else 
        {
            var root = this;
            this.TRACER.ct = 0;
            this.TRACER.onEnterFrame = function ()
            {
                ++this.ct;
                if (this.ct > 15) 
                {
                    root.nextAImove();
                    delete (this.onEnterFrame);
                }
            }
            ;
        }
        this.FIGHTBOX.resetWinner();
    }

    function nextAImove(oldID)
    {
        this.AI.cleverMove();
    }

    function kill()
    {
        delete (this.TRACER.onEnterFrame);
    }

    function clearAll()
    {
        var __reg2 = 0;
        for (;;) 
        {
            if (__reg2 >= this.rubbish_arr.length) 
            {
                return;
            }
            this.rubbish_arr[__reg2].removeMovieClip();
            ++__reg2;
        }
    }

    function random2(z)
    {
        return Math.round(Math.random() * z);
    }

    function random3(z)
    {
        var __reg4 = new Array();
        var __reg3 = 0;
        while (__reg3 <= z) 
        {
            var __reg2 = 0;
            while (__reg2 <= __reg3) 
            {
                __reg4.push(__reg3);
                ++__reg2;
            }
            ++__reg3;
        }
        return __reg4[this.random2(__reg4.length - 1)];
    }

    function shuffledArray(arr)
    {
        var __reg5 = 0;
        while (__reg5 < 20) 
        {
            var __reg4 = this.random2(arr.length - 1);
            var __reg3 = this.random2(arr.length - 1);
            var __reg6 = arr[__reg4];
            arr[__reg4] = arr[__reg3];
            arr[__reg3] = __reg6;
            ++__reg5;
        }
        return arr;
    }

    function countPlayerUnits(terrArr, pl)
    {
        var __reg3 = 0;
        var __reg1 = 0;
        while (__reg1 < terrArr.length) 
        {
            if (terrArr[__reg1].player == pl) 
            {
                __reg3 = __reg3 + terrArr[__reg1].unitsNO;
            }
            ++__reg1;
        }
        return __reg3;
    }

}
